package Demo5;

import java.lang.reflect.Array;
import java.sql.SQLOutput;
import java.util.Arrays;

class Student implements Comparable<Student>{//实现Comparable接口需要重写compareto方法
    public int age;
    public String name;
    public float score;
    public Student (String name,int age,float score){
        this.age=age;
        this.name=name;
        this.score=score;
    }

    @Override
    public String toString() {
        return "student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }


    @Override
    public int compareTo(Student o) {
        //假如按年龄大小比较
        /*if (this.age>o.age){
            return 1;
        } else if (this.age < o.age) {
            return -1;
        }else {
            return 0;
        }*/
        //假如按字符串比较
        if (this.name.compareTo(o.name)>0){
            return 1;
        } else if (this.name.compareTo(o.name)<0) {
            return -1;
        }else {
            return 0;
        }
    }


}

public class Test {
    //实现了Comparable接口的数组都能传进来
    public static void sort(Comparable[] array){
        for (int i=0;i< array.length;i++){
            for (int j=0;j< array.length-1-i;j++){
                if (array[j].compareTo(array[j+1])>0){
                    Comparable tmp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=tmp;
                }
            }
        }

    }
    public static void main(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student("laowu",45,45.23F);
        students[1]=new Student("zhangsan",25, 78.23F);
        students[2]=new Student("lsi",43,123.34F);
        System.out.println(students[0].compareTo(students[1]));
        sort(students);
        System.out.println(Arrays.toString(students));
    }
}
